package vn.conghuy.talkwithme.responsor.subresponsor;

import vn.conghuy.talkwithme.MyApp;
import vn.conghuy.talkwithme.R;
import vn.conghuy.talkwithme.data.sqlmodel.SqlData;
import vn.conghuy.talkwithme.responsor.responsor.MapResponsor;
import vn.conghuy.talkwithme.utils.DoubleWrapper;
import vn.conghuy.talkwithme.utils.StringWrapper;
import vn.conghuy.talkwithme.utils.Utils;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

public class NavigateMapSubResponsor {
	
	private static final String TO_KEYWORD = "to";
	private static final String FROM_KEYWORD = "from";
	
	private static final int RELATIVE_NONE = 0;
	private static final int RELATIVE_REINPUT_ROUTE = 1;
	public static final int RELATIVE_CONFIRM = 2;
	
	int relativeWithLastResponse;
	MapResponsor mapResponsor;
	MyApp myApp;
	Context context;
	double fromLatitude;
	double fromLongtitude;
	double toLatitude;
	double toLongtitude;
	String fromName;
	String toName;
	
	public NavigateMapSubResponsor(MapResponsor mapResponsor, Context context) {
		this.mapResponsor = mapResponsor;
		this.context = context;
		myApp = (MyApp)context.getApplicationContext();
	}
	
	public String response(String request){
		switch (relativeWithLastResponse) {
		case RELATIVE_NONE:
			// check request has navigate-keyword, "to" and "from" keyword, with that order.
			//TODO : more accuracy by : 1. find navigate keyword; 2. find from keyword is the first index after navigate keyword
			int toIndex = Utils.lastIndexOfWord(request, TO_KEYWORD);
			int fromIndex = Utils.lastIndexOfWord(request, FROM_KEYWORD);
			StringWrapper navigateKeywordWrapper = new StringWrapper();
			if(Utils.isExistElemInArrayIsWordOfStr(SqlData.mapNavigateKeywords, request, navigateKeywordWrapper)
					&& toIndex != -1 && fromIndex != -1 && toIndex > fromIndex){
				String navigateKeyword = navigateKeywordWrapper.getStr();
				int navigateKeywordIndex =  Utils.indexOfWord(request, navigateKeyword);
				if(navigateKeywordIndex < toIndex){
					// find from phrase and to phrase
					int fromPhraseIndex = fromIndex + FROM_KEYWORD.length() + 1;
					int toPhraseIndex = toIndex + TO_KEYWORD.length() + 1;
					String fromPhrase = request.substring(fromPhraseIndex, toIndex - 1);
					String toPhrase = request.substring(toPhraseIndex);
					
					return parseRoutePhraseAndSetRelative(fromPhrase, toPhrase);
					
				}
			}
			break;

		case RELATIVE_CONFIRM:
			if(Utils.isExistElemInArrayIsWordOfStr(SqlData.callYesWords, request, null)){
				relativeWithLastResponse = RELATIVE_NONE;
				mapResponsor.setRelativeLastResponse(MapResponsor.RELATIVE_NONE);
				navigate();
				return context.getString(R.string.navigate_command);
			}
			else if(Utils.isExistElemInArrayIsWordOfStr(SqlData.callNoWords, request, null)){
				toIndex = Utils.lastIndexOfWord(request, TO_KEYWORD);
				fromIndex = Utils.lastIndexOfWord(request, FROM_KEYWORD);
				if(toIndex != -1 && fromIndex != -1 && fromIndex < toIndex){
					int fromPhraseIndex = fromIndex + FROM_KEYWORD.length() + 1;
					int toPhraseIndex = toIndex + TO_KEYWORD.length() + 1;
					String fromPhrase = request.substring(fromPhraseIndex, toIndex - 1);
					String toPhrase = request.substring(toPhraseIndex);
					return parseRoutePhraseAndSetRelative(fromPhrase, toPhrase);
				}
				else{
					relativeWithLastResponse = RELATIVE_REINPUT_ROUTE;
					mapResponsor.setRelativeLastResponse(MapResponsor.RELATIVE_NAVIGATE);
					return Utils.getRandomString(SqlData.mapAskReinputAddress);
				}
			}
			else{
				toIndex = Utils.lastIndexOfWord(request, TO_KEYWORD);
				fromIndex = Utils.lastIndexOfWord(request, FROM_KEYWORD);
				if(toIndex != -1 && fromIndex != -1 && fromIndex < toIndex){
					int fromPhraseIndex = fromIndex + FROM_KEYWORD.length() + 1;
					int toPhraseIndex = toIndex + TO_KEYWORD.length() + 1;
					String fromPhrase = request.substring(fromPhraseIndex, toIndex - 1);
					String toPhrase = request.substring(toPhraseIndex);
					return parseRoutePhraseAndSetRelative(fromPhrase, toPhrase);
				}
			}
			
		case RELATIVE_REINPUT_ROUTE:
			toIndex = Utils.lastIndexOfWord(request, TO_KEYWORD);
			fromIndex = Utils.lastIndexOfWord(request, FROM_KEYWORD);
			if(toIndex != -1 && fromIndex < toIndex){
				int fromPhraseIndex = 0;
				if(fromIndex != -1){
					fromPhraseIndex = fromIndex + FROM_KEYWORD.length() + 1;
				}
				int toPhraseIndex = toIndex + TO_KEYWORD.length() + 1;
				String fromPhrase = request.substring(fromPhraseIndex, toIndex - 1);
				String toPhrase = request.substring(toPhraseIndex);
				return parseRoutePhraseAndSetRelative(fromPhrase, toPhrase);
			}
			else{
				relativeWithLastResponse = RELATIVE_REINPUT_ROUTE;
				mapResponsor.setRelativeLastResponse(MapResponsor.RELATIVE_NAVIGATE);
				return Utils.getRandomString(SqlData.mapAskReinputAddress);
			}
		}
		return null;
	}
	
	void navigate(){
		String uri = "http://maps.google.com/maps?saddr=" + fromLatitude + "," + fromLongtitude +"&daddr=" + toLatitude + "," + toLongtitude;
		Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
		context.startActivity(intent);

	}
	
	String parseRoutePhraseAndSetRelative(String from, String to){
		boolean result = true;
		
		DoubleWrapper fromLatitudeWrapper = new DoubleWrapper();
		DoubleWrapper fromLongtitudeWrapper = new DoubleWrapper();
		StringWrapper fromNameWrapper = new StringWrapper();
		vn.conghuy.talkwithme.android.utils.Utils.searchFromName(from, context, fromLatitudeWrapper, fromLongtitudeWrapper, fromNameWrapper);
		fromLatitude = fromLatitudeWrapper.getDouble();
		fromLongtitude = fromLongtitudeWrapper.getDouble();
		fromName = fromNameWrapper.getStr();
		if(fromName == null) result = false;
		
		if(result == true){
			DoubleWrapper toLatitudeWrapper = new DoubleWrapper();
			DoubleWrapper toLongtitudeWrapper = new DoubleWrapper();
			StringWrapper toNameWrapper = new StringWrapper();
			vn.conghuy.talkwithme.android.utils.Utils.searchFromName(to, context, toLatitudeWrapper, toLongtitudeWrapper, toNameWrapper);
			toLatitude = toLatitudeWrapper.getDouble();
			toLongtitude = toLongtitudeWrapper.getDouble();
			toName = toNameWrapper.getStr();
			if(toName == null) result = false;
		}
		
		if(result == true){
			relativeWithLastResponse = RELATIVE_CONFIRM;
			mapResponsor.setRelativeLastResponse(MapResponsor.RELATIVE_NAVIGATE);
			return Utils.getRandomString(SqlData.mapAskLocationConfirm) + "\n" 
				+ FROM_KEYWORD + " "+  fromName + "\n"
				 + TO_KEYWORD + " " + toName;
		}
		else{
			mapResponsor.setRelativeLastResponse(MapResponsor.RELATIVE_NAVIGATE);
			relativeWithLastResponse = RELATIVE_REINPUT_ROUTE;
			return Utils.getRandomString(SqlData.mapAskReinputAddress);
		}
	}
	
	public void reset() {
		fromLatitude = 0;
		fromLongtitude = 0;
		toLatitude = 0;
		toLongtitude = 0;
		fromName = null;
		toName = null;
	}
}
